﻿using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Net;

namespace SDT.Lib.Mail
{
    /// <summary>
    /// Written by DongDD
    /// </summary>
    public class SmtpMailer
    {
        public static void sendMail(string to, string from, string subject, string body)
        {
            /// Using default Smtp config (config in Web.config)
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;

            /// Mail details
            MailMessage msg = new MailMessage(from,to);

            try
            {
				msg.From = new MailAddress(from, "FTCLand");
                msg.To.Add(to);
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Subject = subject;
                msg.IsBodyHtml = true;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.Body = body;
                msg.Priority = MailPriority.Normal;
                
                /// Enable one of the following method.
                client.Send(msg);
            }
            catch (Exception exp)
            {
                /// Throw exception to higher tier
                throw exp;
            }
        }

        public static void sendMail(string to, string from,string pwdfrom, string subject, string body, int port, string host)
        {
            /// Using default Smtp config (config in Web.config)
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Port = port;
            client.Host = host;
            client.Credentials = new NetworkCredential(from, pwdfrom);
            /// Mail details
            MailMessage msg = new MailMessage(from, to);

            try
            {
                msg.From = new MailAddress(from, "FTC Land");
                msg.To.Add(to);
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Subject = subject;
                msg.IsBodyHtml = true;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.Body = body;
                msg.Priority = MailPriority.Normal;

                /// Enable one of the following method.
                client.Send(msg);
            }
            catch (Exception exp)
            {
                /// Throw exception to higher tier
                throw exp;
            }
        }
        
        public static void sendMail(IList<string> to, string from, string subject, string body)
        {
            foreach (var t in to)
                sendMail(t, from, subject, body);
        }
    }
}